import java.awt.Color;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.Timer;


public class MainThing implements GameStuff{
	//bumper + bounce

	double x = 490.0;
	double y = 642.0;
	double x2 = 490.0;
	double y2 = 40.0;
	Color c;
	

	@Override
	public void actionPerformed(ActionEvent e){
		move();
		bounce();
		score();
		PowerUp();
	}
	
	public boolean moveUp;
	public boolean moveDown;
	public boolean moveLeft;
	public boolean moveRight;
	public boolean moveLeft2;
	public boolean moveRight2;
	public boolean speedP1;
	public boolean speedP2;
	public boolean speedUse1 = true;
	public boolean speedUse2 = true;
	public boolean win1;
	public boolean win2;
	public int circleX = 550;
	public int circleY = 640;
	public int moveCircleX = 0;
	public int moveCircleY = -5;
	public int scoreP1;
	public int scoreP2;
	public int powerupCount1;
	public int powerupCount2;
	public Timer t;
	public Timer t2;

	@Override
	public void keyPressed(KeyEvent e){
		if(e.getKeyCode() == KeyEvent.VK_UP){
			moveUp = true;
			//y -= 5;
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN){
			moveDown = true;
			//y += 5;
		}
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			moveLeft = true;
			//x -= 5;
		}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			moveRight = true;
			//x += 5;
		}
		
		
		if(e.getKeyCode() == KeyEvent.VK_A){
			moveLeft2 = true;
		}
		if(e.getKeyCode() == KeyEvent.VK_D){
			moveRight2 = true;
		}
		
		if(speedUse1){
			if(e.getKeyCode() == KeyEvent.VK_UP){
				speedP1 = true;
			}
		}
		if(speedUse2){
			if(e.getKeyCode() == KeyEvent.VK_W){
				speedP2 = true;
			}
		}
	}
	@Override
	public void keyReleased(KeyEvent e){
//		if(e.getKeyCode() == KeyEvent.VK_UP){
//			moveUp = false;
//		}
//		if(e.getKeyCode() == KeyEvent.VK_DOWN){
//			moveDown = false;
//		}
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			moveLeft = false;
		}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			moveRight = false;
		}
		
		if(e.getKeyCode() == KeyEvent.VK_A){
			moveLeft2 = false;
		}
		if(e.getKeyCode() == KeyEvent.VK_D){
			moveRight2 = false;
		}
	}
	@Override
	public void keyTyped(KeyEvent e){
		
	}
	
	public void move(){
		if(x <= 200){					//wall borders
			moveLeft = false;
		}
		if(x + 135 >= 818){
			moveRight = false;
		}
		if(x2 <= 200){
			moveLeft2 = false;
		}
		if(x2 + 135 >= 818){
			moveRight2 = false;
		}
//		if(y <= 0){
//			moveUp = false;
//		}
//		if(y + 78 >= 720){
//			moveDown = false;
//		}
		
//		if(moveUp){					//large square move
//			y -= 6;
//		}
//		if(moveDown){
//			y += 6;
//		}
		if(moveLeft){
			x -= 6;
		}
		if(moveRight){
			x += 6;
		}
		
		if(moveLeft2){
			x2 -= 6;
		}
		if(moveRight2){
			x2 += 6;
		}
		
		
		circleX += moveCircleX;
		circleY += moveCircleY;
	}
	
	public void bounce(){
		if((circleX >= x-15 && circleX <= x+40) && (circleY+20 >= y && circleY <= y+20)){			//top left   30, 70, 30
			moveCircleY = -6;
			moveCircleX = -6;
		}
		if((circleX >= x+35 && circleX+20 <= x+105) && (circleY+20 >= y && circleY <= y+20)){		//top middle
			moveCircleY = -5;
			if(moveCircleX > 0){
				moveCircleX = 2;
			}
			else{
				moveCircleX = -2;
			}
		}
		if((circleX >= x+100 && circleX <= x+140) && (circleY+20 >= y && circleY <= y+20)){		//top right
			moveCircleY = -6;
			moveCircleX = 6;
		}
		
		if((circleX >= x2-15 && circleX <= x2+40) && (circleY <= y2+10 &&  circleY >= y2-10)){			//top2 left   30, 70, 30
			moveCircleY = 6;
			moveCircleX = -6;
		}
		if((circleX >= x2+35 && circleX+20 <= x2+105) && (circleY <= y2+10 &&  circleY >= y2-10)){		//top2 middle
			moveCircleY = 5;
			if(moveCircleX > 0){
				moveCircleX = 2;
			}
			else{
				moveCircleX = -2;
			}
		}
		if((circleX >= x2+100 && circleX <= x2+135) && (circleY <= y2+10 &&  circleY >= y2-10)){		//top2 right
			moveCircleY = 6;
			moveCircleX = 6;
		}
		
//		if((circleX >= x-10 && circleX+20 <= x+60) && (circleY-5 <= y+50 && circleY-5 >= y+40)){			//bottom
//			moveCircleY = 5;
//		}
//		if((circleY-5 >= y-10 && circleY+20 <= y+60) && (circleX+20 >= x && circleX+20 <= x+10)){			//left
//			moveCircleX = -5;
//		}
//		if((circleY-5 >= y-10 && circleY+20 <= y+60) && (circleX-5 <= x+50 && circleX-5 >= x+40)){			//right
//			moveCircleX = 5;
//		}
		
		if(circleX - 5 <= 60){		//wall bounce
			moveCircleX = 5;
		}
		if(circleX + 20 >= 954){
			moveCircleX = -5;
		}
		if(circleY <= 50 && (circleX + 10 <= 200)){
			moveCircleY = 5;
		}
		if(circleY <= 53 && (circleX >= 824)){
			moveCircleY = 5;
		}
		if(circleY + 20 >= 655 && (circleX + 10 <= 200)){
			moveCircleY = -5;
		}
		if(circleY + 20 >= 655 && (circleX >= 824)){
			moveCircleY = -5;
		}
//		if(circleY <= 0){
//			moveCircleY = 5;
//		}
		
//		if(circleY >= 720){
//			moveCircleY = -5;
//		}
		
		
		if(circleX <= 200 && circleY >= 655){
			moveCircleX = 5;
		}
		if(circleX >= 818 && circleY >= 655){
			moveCircleX = -5;
		}
		if(circleX >= 818 && circleY <= 50){
			moveCircleX = -5;
		}
		if(circleX < 200 && circleY <50){
			moveCircleX = 5;
		}
	}
	
	
	public void PowerUp(){
		if(speedP1){
			moveCircleX *= 3;
			moveCircleY *= 3;
			speedP1 = false;
			speedUse1 = false;
		}
		if(speedP2){
			moveCircleX *= 3;
			moveCircleY *= 3;
			speedP2 = false;
			speedUse2 = false;
		}
		
//		if(powerupCount1 > 0){
//			speedUse1 = true;
//			powerupCount1--;
//		}
//		if(powerupCount2 > 0){
//			speedUse2 = true;
//			powerupCount2--;
//		}
	}
	
//	public void powerUpCount(){
//		if(scoreP1 % 3 == 0){
//			powerupCount1++;
//		}
//	}
	
	
	public void score(){
		if(circleY >= 720){
			scoreP2++;
			circleX = 550;
			circleY = 70;
			x = 490.0;
			x2 = 490.0;
			
			if(scoreP2 % 2 == 0){
				moveCircleX = 1;
			}else{
				moveCircleX = -1;
			}
			moveCircleY = 5;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(circleY <= -30){
			scoreP1++;
			circleX = 550;
			circleY = 640;
			x = 490.0;
			x2 = 490.0;
			
			if(scoreP1 % 2 == 0){
				moveCircleX = 1;
			}else{
				moveCircleX = -1;
			}
			moveCircleY = -5;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(scoreP1 == 11){
			win1 = true;
		}
		if(scoreP2 == 11){
			win2 = true;
		}
	}
	
	

		@Override
		public Shape getP1() {
			// TODO Auto-generated method stub
			return new Rectangle2D.Double(x, y, 130, 10);
		}
		
		@Override
		public Shape getP2(){
			return new Rectangle2D.Double(x2, y2, 130, 10);
		}
		
		@Override
		public Shape getCircle(){
			return new Rectangle2D.Double(circleX, circleY, 20, 20);
		}

		@Override
		public Color getColor() {
			// TODO Auto-generated method stub
			return new Color(0, 0, 255);
		}

		@Override
		public double getX() {
			// TODO Auto-generated method stub
			return x;
		}

		@Override
		public double getY() {
			// TODO Auto-generated method stub
			return y;
		}
		
		@Override
		public String getScoreP1(){
			String SscoreP1 = Integer.toString(scoreP1);
			return SscoreP1;
		}
		
		@Override
		public String getScoreP2(){
			String SscoreP2 = Integer.toString(scoreP2);
			return SscoreP2;
		}
		
		@Override
		public boolean getSpeedP1(){
			return speedUse1;
		}
		
		@Override
		public boolean getSpeedP2(){
			return speedUse2;
		}
		
		@Override
		public boolean getWin1(){
			return win1;
		}
		
		@Override
		public boolean getWin2(){
			return win2;
		}
		
		
	}
